/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.sd.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.modules.sd.common.constants.GlobalEnum;
import com.jeesite.modules.sd.entity.DocRecv;
import com.jeesite.modules.sd.service.DocRecvService;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.UserService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sd.entity.DocDeliverRecord;
import com.jeesite.modules.sd.service.DocDeliverRecordService;

/**
 * 文件复制Controller
 * 拟办还文列表
 * @author ysj
 * @version 2020-07-01
 */
@Controller
@RequestMapping(value = "${adminPath}/sd/docDeliverReturn")
public class DocDeliverReturnController extends BaseController {

    @Autowired
    private DocDeliverRecordService recordService;
   @Autowired
    private DocRecvService docRecvService;
   @Autowired
   private UserService userService;
    /**
     * 获取数据
     */
    @ModelAttribute
    public DocDeliverRecord get(String id, boolean isNewRecord) {
        return recordService.get(id, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("sd:docDeliverReturn:view")
    @RequestMapping(value = {"list", ""})
    public String list(DocDeliverRecord docDeliverRecord, Model model) {

        model.addAttribute("docDeliverRecord", docDeliverRecord);
        return "modules/sd/docDeliverReturnList";
    }

    /**
     * 拟办批量还文
     * @param userCodes
     * @param data
     * @param userPolice  临时还文人警号
     * @param userName  临时还文人姓名
     * @return
     */

    @RequiresPermissions("sd:docDeliverReturn:edit")
    @RequestMapping(value="batchFiles")
    @ResponseBody
    public String batchFiles(String userCodes,String[] data,String userPolice,String userName,String face) {
       if(!"".equals(userCodes)){
        User user = userService.get(userCodes);
       if(data.length==0){
           return renderResult(Global.FALSE,text("归还失败"));
       }else{
           for(int i=0;i<data.length;i++){
               DocDeliverRecord docDeliverRecord = recordService.get(data[i]);
               String docId= docDeliverRecord.getDocId();
                 recordService.returnFile(docId,data[i],user);
           }
           return renderResult(Global.TRUE,text("归还成功")); }
       }else{
           //临时归还文件
           for(int i=0;i<data.length;i++){
               DocDeliverRecord docDeliverRecord = recordService.get(data[i]);
               String docId= docDeliverRecord.getDocId();
               recordService.temporaryReturnFile(userName,userPolice,docId,data[i],face);

           }

           return renderResult(Global.TRUE,text("归还成功"));

     }


    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("sd:docDeliverReturn:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<DocDeliverRecord> listData(DocDeliverRecord docDeliverRecord, HttpServletRequest request, HttpServletResponse response) {
        docDeliverRecord.setPage(new Page<>(request, response));
        docDeliverRecord.setDeliverType(GlobalEnum.RECORD_DELIVER_TYPE_PROPOSED.getKey());
        docDeliverRecord.getSqlMap().getWhere().and("take_date", QueryType.IS_NOT_NULL,null);
        docDeliverRecord.getSqlMap().getWhere().and("return_date", QueryType.IS_NULL,null);
        Page<DocDeliverRecord> page = recordService.findPage(docDeliverRecord);
        return page;
    }

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("sd:docDeliverReturn:view")
    @RequestMapping(value = "form")
    public String form(DocDeliverRecord docDeliverRecord, Model model) {
        String str=docDeliverRecord.getDocRecv().getId();
        DocRecv docRecv = docRecvService.get(str);

        model.addAttribute("docRecv", docRecv);
        return "modules/sd/docDeliverHistoryShow";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("sd:docDeliverReturn:edit")
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated DocDeliverRecord docDeliverRecord) {
        //修改原文的状态
        recordService.updateState(docDeliverRecord);
        return renderResult(Global.TRUE, text("保存文件分发成功！"));
    }
    /**
     * 拟办还文
     */
    @RequiresPermissions("sd:docDeliverReturn:edit")
    @PostMapping(value = "returnFile")
    @ResponseBody
    public String returnFile(DocDeliverRecord docDeliverRecord, String userCode, String doi,String showUserId,String policeCode,String face) {
       User user = userService.get(userCode);
        if (ObjectUtils.isNotEmpty(user)){
            try{
                recordService.returnFile(docDeliverRecord.getId(),doi,user);
                return renderResult(Global.TRUE, text("文件归还成功！"));
            }catch (Exception e){
                return renderResult(Global.TRUE, text("文件归还失败！"));
            }

        }else{
            try{
                recordService.temporaryReturnFile(showUserId,policeCode,docDeliverRecord.getId(),doi,face);
                return renderResult(Global.TRUE, text("文件归还成功！"));
            }catch (Exception e){
                return renderResult(Global.TRUE, text("文件归还失败！"));
            }
        }




    }
    /**
     * 查询拟办还文文件首页
     * @param docDeliverRecord
     * @param model
     * @return
     */
    @RequiresPermissions("sd:docDeliverReturn:view")
    @RequestMapping(value = {"index", ""})
    public String index(DocDeliverRecord docDeliverRecord, Model model) {
        model.addAttribute("docDeliverRecord", docDeliverRecord);
        return "modules/sd/docDeliverReturnIndex";
    }

}